package com.feige.utils.txsms;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;

/**
 * @author feige<br />
 * @ClassName: SmsService <br/>
 * @Description: <br/>
 * @date: 2021/8/17 15:28<br/>
 */
@Component
public class SmsService {

    // 短信应用 SDK AppID
    @Value("${sms.appid}")
    private int APPID;

    // 短信应用 SDK AppKey
    @Value("${sms.app-key}")
    private String APP_KEY;

    // 短信模板 ID，需要在短信应用中申请
    @Value("${sms.template-id}")
    private int TEMPLATE_ID;

    @Value("${sms.sms-sign}")
    private String SMS_SIGN;

    public boolean sendSms(String phone, String code){
        try {
            String[] params = {code,"5"};
            SmsSingleSender sSender = new SmsSingleSender(APPID, APP_KEY);
            SmsSingleSenderResult result = sSender.sendWithParam("86", phone,
                    TEMPLATE_ID, params, SMS_SIGN, "", "");
            System.out.println(result);
        } catch (HTTPException | JSONException | IOException | com.github.qcloudsms.httpclient.HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
            return false;
        } // JSON 解析错误
        // 网络 IO 错误
        return true;
    }
}
